package writtenexamination.baorong;
/**
 * @author cyt
 * @create 2020-10-20 下午 4:12
 */
public class Tb {

    public static void main(String[] args) {
        Tb tb = new Tb();
        fff fff = new fff(1);
        writtenexamination.baorong.fff fff1 = new fff(2);

        writtenexamination.baorong.fff fff2 = new fff(4);

        writtenexamination.baorong.fff fff3 = new fff(6);
        fff.next = fff1;

        fff2.next = fff3;

        writtenexamination.baorong.fff merg = tb.merg(fff, fff2);

        while (merg != null) {
            System.out.println(merg.val);
            merg = merg.next;
        }
    }

    public fff merg(fff n, fff m) {
        if (n == null) {
            return m;
        }
        if (m == null) {
            return n;
        }

        fff newhead = new fff(-1);
        fff newtail = newhead;
        fff cur1 = n;
        fff cur2 = m;
        while (cur1 != null && cur2 != null) {
            if (cur1.val < cur2.val) {
                newtail.next = cur1;
                newtail = newtail.next;
                cur1 = cur1.next;
            } else {
                newtail.next = cur2;
                newtail = newtail.next;
                cur2 = cur2.next;
            }
        }

        if (cur1 == null) {
            newtail.next = cur2;
        } else {
            newtail.next = cur1;
        }
        return newhead.next;
    }
}

class fff {
    int val;
    fff next;


    fff() {
    }

    fff(int val) {
        this.val = val;
    }

    fff(int val, fff next) {
        this.val = val;
        this.next = next;
    }
}
